<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Observers;


use App\Jobs\FilterThreadSensitiveWords;
use App\Models\Message;
use Illuminate\Support\Facades\Auth;

/**
 * Class MessageObserver
 * @package App\Observers
 */
class MessageObserver
{
    /**
     * @param Message $model
     */
    public function created(Message $model)
    {
    }

    /**
     * @param Message $model
     */
    public function saving(Message $model)
    {
        if (!$model->user_id) {
            $model->user_id = Auth::guard('api')->id();
        }
        if (!$model->reply_id) {
            $model->reply_id = -1;
        }

        $model->content = \dispatch_now(new FilterThreadSensitiveWords($model->content));
    }

    /**
     * @param Message $model
     */
    public function saved(Message $model)
    {

    }
}
